package com.example.q_kang.hhh.login;

import com.hjq.toast.ToastUtils;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.HttpHeaders;
import com.lzy.okgo.model.HttpParams;
import com.lzy.okgo.model.Response;
import com.sskj.base.BasePresenter;
import com.sskj.base.api.Constant;
import com.sskj.base.api.SpConfig;
import com.sskj.base.base.App;
import com.sskj.base.base.HttpData;
import com.sskj.base.bean.HeaderBean;
import com.example.q_kang.hhh.api.JsonCallBack;
import com.sskj.base.utils.SPUtils;


/**
 * @author Hey
 * Create at  2019/04/11
 */
class LoginPresenter extends BasePresenter<LoginActivity> {

    public void login(String mobile, String password) {
        httpService.login(mobile, password)
                .tag(this)
                .execute(new JsonCallBack<HttpData<HeaderBean>>() {
                    @Override
                    public void onSuccess(Response<HttpData<HeaderBean>> response) {
                        HttpData<HeaderBean> httpData = response.body();
                        if (httpData.getStatus() == Constant.OK) {
                            SPUtils.put(App.INSTANCE, SpConfig.LOGIN, "1");
                            SPUtils.put(App.INSTANCE, SpConfig.MOBILE, mobile);
                            SPUtils.put(App.INSTANCE, SpConfig.TOKEN, httpData.getData().getToken());
                            SPUtils.put(App.INSTANCE, SpConfig.ACCOUNT, httpData.getData().getAccount());
                            HttpHeaders headers = new HttpHeaders();
                            headers.put("systemType", "android");
                            headers.put("account", httpData.getData().getAccount());    //所有的 header 都 不支持 中文
                            headers.put("token", httpData.getData().getToken());
                            HttpParams httpParams = new HttpParams();
                            httpParams.put("systemType", "android");
                            httpParams.put("account", httpData.getData().getAccount());    //所有的 header 都 不支持 中文
                            httpParams.put("token", httpData.getData().getToken());
                            OkGo.getInstance().addCommonHeaders(headers);
                            OkGo.getInstance().addCommonParams(httpParams);
                            mView.loginSuccess();
                        } else {
                            ToastUtils.show(httpData.getMsg());
                        }
                    }
                });
//        Api.getInstance().login(mobile, password)
//                .compose(RxUtils.transform())
//                .subscribe(new ApiObserver<HeaderBean>(this, true) {
//                    @Override
//                    public void onSuccess(HeaderBean data) {
//                        SPUtils.put(App.INSTANCE, "token", data.getToken());
//                        SPUtils.put(App.INSTANCE, "account", data.getAccount());
//                        mView.loginSuccess();
//                    }
//                });
    }

}
